package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.PlayHistoryVo;
import org.dromara.business.domain.bo.PlayHistoryBo;
import org.dromara.business.service.IPlayHistoryService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 统一播放记录
 *
 * @author Hao Tong Xue
 * @date 2025-04-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/playHistory")
public class PlayHistoryController extends BaseController {

    private final IPlayHistoryService playHistoryService;

    /**
     * 查询统一播放记录列表
     */
    @SaCheckPermission("business:playHistory:list")
    @GetMapping("/list")
    public TableDataInfo<PlayHistoryVo> list(PlayHistoryBo bo, PageQuery pageQuery) {
        return playHistoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出统一播放记录列表
     */
    @SaCheckPermission("business:playHistory:export")
    @Log(title = "统一播放记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PlayHistoryBo bo, HttpServletResponse response) {
        List<PlayHistoryVo> list = playHistoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "统一播放记录", PlayHistoryVo.class, response);
    }

    /**
     * 获取统一播放记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:playHistory:query")
    @GetMapping("/{id}")
    public R<PlayHistoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(playHistoryService.queryById(id));
    }

    /**
     * 通过内容id获取统一播放记录详细信息
     *
     * @param contentId 内容id
     * @param contentType 内容类型：1-视频，2-音频
     */
//    @SaCheckPermission("business:playHistory:query")
    @GetMapping("/{contentId}/{contentType}")
    public R<PlayHistoryVo> getInfo(@PathVariable Long contentId,
                                    @PathVariable Integer contentType) {
        return R.ok(playHistoryService.getInfo(contentId, contentType));
    }

    /**
     * 新增统一播放记录
     */
//    @SaCheckPermission("business:playHistory:add")
    @Log(title = "统一播放记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PlayHistoryBo bo) {
        playHistoryService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改统一播放记录
     */
    @SaCheckPermission("business:playHistory:edit")
    @Log(title = "统一播放记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PlayHistoryBo bo) {
        return toAjax(playHistoryService.updateByBo(bo));
    }

    /**
     * 删除统一播放记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:playHistory:remove")
    @Log(title = "统一播放记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(playHistoryService.deleteWithValidByIds(List.of(ids), true));
    }
}
